package cn.dnui_cjy529.dnuifood_16110100529.Fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.dnui_cjy529.dnuifood_16110100529.Adapter.ShopAdapter;
import cn.dnui_cjy529.dnuifood_16110100529.R;
import cn.dnui_cjy529.dnuifood_16110100529.bean.ShopBean;
import cn.dnui_cjy529.dnuifood_16110100529.controller.BaseActivity;
import cn.dnui_cjy529.dnuifood_16110100529.listener.OnRetrofitListener;
import cn.dnui_cjy529.dnuifood_16110100529.model.ShopModel;

//public class HomeFragment extends Fragment {
//    private View view=null;
//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
//        view=inflater.inflate(R.layout.tab01,container,false);
//        return view;
//    }
//}
public class HomeFragment extends android.support.v4.app.Fragment implements OnRetrofitListener<List<ShopBean>> {
    private ShopBean shop;
    private RecyclerView recyclerView;
    private ShopAdapter adapter;
    private BaseActivity baseActivity;
    private SharedPreferences sharedPreferences;
    private String user_id;

    public HomeFragment()  {
        // Required empty public constructor
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recyclerView=view.findViewById(R.id.shop_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(new ShopAdapter(getActivity(),getData()));
        initViews();
        initData();


//       if (getActivity() != null){
//           LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
//           layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//           recyclerView.setLayoutManager(layoutManager);
//           recyclerView.setAdapter(adapter);
//           initData();
//       }


    }

    void initViews(){
        recyclerView = recyclerView.findViewById(R.id.shop_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    void initData(){
        ShopModel shopModel = new ShopModel();
        shopModel.getAllShops(this);  //访问服务器请求
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.tab01, container, false);
    }



    private List<ShopBean> getData(){
        List<ShopBean> list = new ArrayList<>();
        ShopBean shop = new ShopBean();
        shop.setShopname("满口香川菜");
        shop.setAddress("东软食堂三期");
        shop.setPic(String.valueOf(R.drawable.i1));
        shop.setLevel(5);
        list.add(shop);

        shop = new ShopBean();
        shop.setShopname("东北一家人");
        shop.setAddress("东软食堂三期");
        shop.setPic(String.valueOf(R.drawable.i2));
        shop.setLevel(3);
        list.add(shop);

        shop = new ShopBean();
        shop.setShopname("姐妹麻辣烫");
        shop.setAddress("东软食堂三期");
        shop.setPic(String.valueOf(R.drawable.i3));
        shop.setLevel(4);
        list.add(shop);

        shop = new ShopBean();
        shop.setShopname("西湖印象");
        shop.setAddress("东软食堂三期");
        shop.setPic(String.valueOf(R.drawable.i4));
        shop.setLevel(5);
        list.add(shop);

        shop = new ShopBean();
        shop.setShopname("丸子铺");
        shop.setAddress("东软食堂三期");
        shop.setPic(String.valueOf(R.drawable.i5));
        shop.setLevel(3);
        list.add(shop);
        return list;
    }

    @Override
    public void onSuccess(List<ShopBean> shops, int flag) {
        adapter = new ShopAdapter(getActivity(),shops);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(getActivity(),"网络错误",Toast.LENGTH_SHORT).show();
    }
}
}